#ifndef __APP_COMM_H_
#define __APP_COMM_H_

#include "APP_Bill.h"

#pragma pack(1)

#define SVR_IP      "114.217.21.111"
#define SVR_PORT    60034


enum EN_COMM_FSM{
    COMM_FSM_INIT=0,
    COMM_FSM_LOGON,
    COMM_FSM_READY,
    COMM_FSM_ABNORMAL,
    COMM_FSM_MAX
};
typedef unsigned char BYTE;
typedef unsigned short WORD;

//开启充电方式
enum EN_START_CHG_WAY{
    CHG_SWIPE_CARD_WAY=0x01, //表示通过刷卡启动充电
    CHG_ACCOUNT_WAY=0x02, //表求通过帐号启动充电 (暂不支持)
    CHG_VIN_WAY=0x03, //表示vin码启动充电
    CHG_MAX_WAY
};

//交易标识
enum EN_TRN_ID{
    TRN_ID_APP=0x01,//app 启动
    TRN_ID_CARD=0x02,//卡启动
    TRN_ID_OFFLINE_CARD=0x04,//离线卡启动
    TRN_ID_VIN0x05,//vin 码启动充电
    TRN_ID_UNKNOW
};

enum EN_CMD{
    CMD_LOGIN_SND=0x01,
    CMD_LOGIN_ACK=0x02,
    CMD_HB_SND=0x03,
    CMD_HB_ACK=0x04,
    CMD_BILLMODE_CHK_SND=0x05,
    CMD_BILLMODE_CHK_ACK=0x06,
    CMD_BILLMODE_REQ_SND=0x09,
    CMD_BILLMODE_REQ_ACK=0x0A,
    CMD_MONITOR_REQ=0x12,
    CMD_MONITOR_RSP=0x13,
    CMD_ACTIVE_START_CHG_REQ=0x31,
    CMD_ACTIVE_START_CHG_REQ_ACK=0x32,
    CMD_REMOTE_START_CHG_SET=0x34,
    CMD_REMOTE_START_CHG_SET_RSP=0x33,
    CMD_REMOTE_STOP_CHG_SET=0x36,
    CMD_REMOTE_STOP_CHG_SET_RSP=0x35,
    CMD_CHG_RECORD_SND=0x3B,
    CMD_CHG_RECORD_ACK=0x40,
    CMD_REMAIN_MONEY_UPDATE_RSP=0x41,
    CMD_REMAIN_MONEY_UPDATE=0x42,
    CMD_TIME_SET_RSP=0x55,
    CMD_TIME_SET=0x56,
    CMD_REMOTE_REBOOT_RSP=0x91,
    CMD_REMOTE_REBOOT=0x92,
    CMD_QRCODE_SET_RSP=0x9B,
    CMD_QRCODE_SET=0x9C,
    CMD_MAX
};

/*IDX与数组索引对应*/
enum EN_CMD_IDX{
    LOGIN_IDX=0x00,
    HB_IDX,
    BILLMODE_CHK_IDX,
    BILLMODE_REQ_IDX,
    MONITOR_IDX,
    ACTIVE_START_CHG_IDX,
    REMOTE_START_CHG_IDX,
    REMOTE_STOP_CHG_IDX,
    CHG_RECORD_IDX,
    REMAIN_MONEY_UPDATE_IDX,
    TIME_IDX,
    REMOTE_REBOOT_IDX,
    QRCODE_IDX,
    UNKNOW_IDX
};

enum EN_CMD_RSLT
{
    CMD_RSLT_UNEXCE=-2,
    CMD_RSLT_WAIT,
    CMD_RSLT_SUCC,
    CMD_RSLT_TIMEOUT,
    CMD_RSLT_ERROR,
    CMD_RSLT_UNKNOW
};

typedef struct{
    enum EN_CMD_IDX cmdidx;
    enum EN_CMD scmd;
    enum EN_CMD rcmd;
    U32 stick;
    U32 rtick;
    U32 timeout;//超时时间，单位ms
    int (*psend)(void *para);//主动上报
    int (*prsp)(enum EN_CMD_IDX cmdidx, U8 err, U16 seqno, U8 *data, U8 dlen);//回复服务器
    int (*precv)(enum EN_CMD_IDX cmdidx, U16 seqno, U8 *data, U8 dlen);//服务器下发确认或命令
    enum EN_CMD_RSLT result;
    U8 code;
    int (*eAction)(enum EN_CMD_IDX cmdidx);//主动上报返回错误后的处理函数
}CMD_ELEMENT_ST;

typedef struct {
    U8 code;
    char *desc;
}AUTH_FAIL_REASON_ST;

typedef struct {
    U8 gun_num;
    BILL_STATS_ST *stats;
}CHG_RECORD_PARA_ST;

#pragma pack()

extern CMD_ELEMENT_ST CmdTbl[UNKNOW_IDX];

int CommMonitorRpt(void *para);
//gun_num:1,2
int CommChgRecord(void *para);
int CommActiveStartChgReq(void *para);
enum EN_COMM_FSM CommFsmGet(void);
int CommLoop(void);


#endif

